package com.lct.todo.servlet;

import com.google.gson.Gson;
import com.lct.todo.daoimpl.TaskDaoImpl;
import com.lct.todo.response.TaskDeleteResponse;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

public class DeleteTaskServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException {
        //基本设置
        request.setCharacterEncoding("utf-8");
        response.setCharacterEncoding("utf-8");
        response.setHeader("Access-Control-Allow-Origin", "*");
        PrintWriter printWriter = response.getWriter();
        //删除
        TaskDeleteResponse deleteResponse;
        String taskId = request.getParameter("taskId");
        int result = new TaskDaoImpl().delete(taskId);
        if (result == 1) {
            deleteResponse = new TaskDeleteResponse(true, "");
        } else {
            deleteResponse = new TaskDeleteResponse(false, "系统异常");
        }
        printWriter.println(new Gson().toJson(deleteResponse));
        printWriter.flush();
        printWriter.close();
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException {
        this.doPost(request, response);
    }
}
